﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.SanXuat.UTI;
using ThaiHoa.Win.QLSX.DAL;
using Excel = Microsoft.Office.Interop.Excel;

namespace ThaiHoa.Win.QLSX.SanXuat.UI
{
    public partial class frmBoPhanList : Form
    {
        #region Fields
        KhoDBContext db;

        BindingSource BoPhanBS;
        Access acs;

        #endregion
        #region Constructor
        public frmBoPhanList()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            db.Saved += new EventHandler(db_Saved);
            BoPhanBS = new BindingSource();
            acs = new Access();
            db.BoPhan.Where(m => m.TrangThai).Load();
            BoPhanBS.DataSource = db.BoPhan.Local.ToBindingList();
            dgvBoPhan.DataSource = BoPhanBS;
            boPhanBN.BindingSource = BoPhanBS;
            CustomerGridView();
        }

        private void ReLoadData()
        {
            if (db != null) db.Dispose();
            KhoiTao();
        }

        private void CustomerGridView()
        {
            //Ẩn toàn bộ columns
            foreach (DataGridViewColumn item in dgvBoPhan.Columns)
            {
                item.Visible = false;
            }
            //Khóa tính năng thêm mới
            dgvBoPhan.AllowUserToAddRows = false;
            //Tùy chỉnh lại

            //Tên bộ phận
            var c = dgvBoPhan.Columns["TenBoPhan"];
            c.Visible = true;
            c.Width = 200;
            c.DisplayIndex = 1;

            //Mô tả
            c = dgvBoPhan.Columns["MoTa"];
            c.Visible = true;
            c.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            c.DisplayIndex = 2;

        }

        private void HienThiChiTiet()
        {
            BoPhan item = BoPhanBS.Current as BoPhan;
            if (item != null)
            {
                frmBoPhanDetail _frmBoPhanDetail = new frmBoPhanDetail();
                _frmBoPhanDetail.DBChanged += new EventHandler(_frmBoPhanDetail_DBChanged);
                acs.ShowFormBoPhanDetail(_frmBoPhanDetail, item.Id);
            }
        }

        private void Xoa()
        {
            BoPhan item = BoPhanBS.Current as BoPhan;
            if (Common.ShowMessengerConfirmDelete() == DialogResult.Yes)
            {
                if (item != null)
                {
                    db.BoPhan.Remove(item);
                    db.SaveChanges();
                }
            }
        }

        private void LuuLai()
        {
            if (Common.ShowMessengerConfirmSave() == DialogResult.Yes)
            {
                db.SaveChanges();
            }
        }
        private void ExportToExcel()
        {
            string workbookPath = Application.StartupPath + @"\Template\Excel\JAT_BoPhan";
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(workbookPath,
               0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);
            Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
            xlWorkBook.Activate();
            xlWorkSheet.Name = "BoPhan";
            //Add data
            var listBoPhan = BoPhanBS.List;
            int count = 0;
            int positonStart = 11;
            Excel.Range r;
            foreach (BoPhan item in listBoPhan)
            {
                xlWorkSheet.Cells[positonStart + count, 1] = count + 1;
                xlWorkSheet.Cells[positonStart + count, 2] = item.TenBoPhan;
                xlWorkSheet.Cells[positonStart + count, 3] = item.MoTa;

                //Format it               

                string start = "A" + (count + positonStart).ToString();
                string stop = "E" + (count + positonStart).ToString();
                r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
                r.Borders.Color = Color.Black;
                r.RowHeight = 30;
                //r.Style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
                //merge
                start = "C" + (count + positonStart).ToString();
                stop = "E" + (count + positonStart).ToString();
                r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
                r.Merge();
                count++;


            }
            xlWorkSheet.Cells[7, 4] = string.Format("Ngày {0}/{1}/{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
            // Hiện chương trình
            xlApp.Visible = true;

            //// Save file
            //xlWorkBook.SaveAs("C:\\JAT_VatTu_DanhMucThepDac_1.xls", Excel.XlFileFormat.xlWorkbookNormal,
            //                null, null, false, false,
            //                Excel.XlSaveAsAccessMode.xlExclusive,
            //                false, false, false, false, false);
            xlApp.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        }
        #endregion
        #region Events

        private void frmBoPhanList_Load(object sender, EventArgs e)
        {
            KhoiTao();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmBoPhanDetail _frmBoPhanDetail = new frmBoPhanDetail();
            _frmBoPhanDetail.DBChanged += new EventHandler(_frmBoPhanDetail_DBChanged);
            acs.ShowFormBoPhanDetail(_frmBoPhanDetail, Guid.Empty);
        }



        private void btnInformation_Click(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvBoPhan_DoubleClick(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LuuLai();
        }

        void db_Saved(object sender, EventArgs e)
        {
            ReLoadData();
        }
        void _frmBoPhanDetail_DBChanged(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        #endregion


    }
}
